<?php
/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/6/13
 * Time: 上午10:24
 */
class BaseOrderService extends BaseService
{
    private $mOrder;
    private $mOrderGoods;     //餐厅id

    public function __construct()
    {
        
        $this->mOrder = D('Base/RestaurantOrder');
        $this->mOrderGoods = D('Base/RestaurantOrderGoods');

    }
    /**
     * 添加订单
     * @param $orderArr
     * @return mixed
     */
    public function addOrder($orderArr)
    {
        //返回添加的最后一条订单
        $orderId = $this->mOrder->add($orderArr);
        
        return $orderId;
    }

    /**
     * 更新订单
     * @param $order_id
     * @param $data
     * @return bool
     */
    public function updateOrder($order_id,$data){


        $res = $this->mOrder->where(array('order_id'=>$order_id))->save($data);
        
        return $res;

    }
    
    public function getOrderInfo($order_id,$filed=''){
        
        $res = $this->mOrder->field($filed)->where(array('order_id'=>$order_id))->limit(1)->find();
        
        return $res;
    }
    
    

    /**
     * 添加订单商品
     * @param $orderGoodsInfo
     * @return bool|string
     */
    public function addGoodsAll($orderGoodsInfo){
        $res = $this->mOrderGoods->addAll($orderGoodsInfo);
        return $res;
    }

    /**
     * 取消订单
     * @param $orderId
     * @return boolean
     */
    public function cancelOrder($orderId)
    {
        return true;
    }

    /**
     * 订单列表
     * @param $resId        订单模块id
     * @param $restaurantId 餐厅id
     * @return mixed;
     */
    public function listOrder($resId,$restaurantId)
    {
        return array();
    }

    /**
     * 打印订单
     * @param $orderId
     */
    public function useWifiPrintInfo($orderId){

        $mOrder = D('Base/RestaurantOrder');
        $mGoods = D('Base/RestaurantGoods');
        $mOrderGoods= D('Base/RestaurantOrderGoods');
        $mWindow = D('Base/RestaurantWindow');
        //查询订单信息
        $info = $mOrder->relation(true)->getByPk($orderId);
        
        if($info){

            $windowInfo = array();

            $windowIdArray = $mWindow->where(array('restaurant_id'=>$info['restaurant_id'],'cut_off'=>0))->getField('window_id',true);

            foreach($windowIdArray as $window_id){
                //查询菜品对应的种类对应档口及打印机信息

                $wInfo = $mWindow->getByPk($window_id);
                //$wInfo = $this->mWindow->getInfo($list['window_id']);
                $windowInfo[$wInfo['window_id']]['print_dno'] = $wInfo['print_dno'];
            }

            if($windowIdArray){
                // 获取订单商品id
                $orderGoodsArray = $mOrderGoods->field('goods_id,goods_num,goods_price,goods_name')->where(array('order_id'=>$orderId))->select();

                $headerStr = '';

                $content = $headerStr.
                    '|5================================'.'\r\n'.
                    '餐厅名：'.$info['RestaurantInfo']['restaurant_name'].'\r\n'.
                    '订单号：'.$info['order_sn'].'\r\n'.
                    '总计：'.$info['order_amount'].'\r\n'.
                    '菜单信息：\r\n';
                foreach ($orderGoodsArray as $key=>$value){

                    $price= $value['goods_price']*$value['goods_num'];

                    $content = $content.
                        $value['goods_name'].'  × '.$value['goods_num'].'  '.$value['goods_price']*$value['goods_num'].'\r\n';
                }
                $content =$content.
                    '欢迎下次惠顾 ! \r\n';

                foreach($windowIdArray as $window_id){
                    $wInfo = $mWindow->getByPk($window_id);
                    $printParams['dno'] = $wInfo['print_dno'];
                    $printParams['content'] = $content;
                    $printParams['print_times'] = 1;
                    DoPrint::getInstance($printParams)->useWifiPrint();
                }
            }
        }
    }


            //判断是否都有西餐和料理
    public function haveWestAndDeal($order_id=''){
        if(!$order_id){
            return false;
        }else{
            $info = $this->mOrderGoods->field('window_id')->where(array('order_id'=>$order_id))->select();
            // echo $this->mOrderGoods->_sql();
            if($info){
                $have_west=0;
                $have_deal=0;
                foreach ($info as $key => $value) {
                    # code...
                    if($value['window_id']==49){
                        $have_west=1;
                    }

                    if ($value['window_id']==56) {
                        # code...
                        $have_deal=1;
                    }

                }

                if($have_west==1&&$have_deal==1){
                    return true;
                }
            }
            return false;
        }
    }

}